package dao.hibernate;

import dao.common.UserManager;
import models.shop.User;
import models.shop.UserRole;

import java.util.List;

public class UserManagerHB implements UserManager {
    @Override
    public User getByEmail(String email) {
        return User.find("select u from User u where u.userInfo.email = ?", email).first();
    }

    @Override
    public User getAnonymousUser(String sessionId) {
        User user = User.find("select u from User u where u.sessionId = ?", sessionId).first();
        if (user == null){
            user = new User();
            user.sessionId = sessionId;
            user.save();
        }
        return user;
    }

    @Override
    public void updateProfile(User user) {
        user.userInfo.save();
        //user.save();
    }

    @Override
    public List<User> getAdmins() {
        UserRole userRole = UserRole.find("byTitle", "admin").first();
        return userRole.users;
    }
}
